// pages/user/order/orderList.js
import { FindUserOrders } from '../../../api/order.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentOrderStatus: null,
    listData: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    FindUserOrders({
      orderStatus: this.data.currentOrderStatus,
    }).then(resp => {
      this.setData({
        listData: resp.content,
      });
    }).catch(err => {
      console.log(err);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    FindUserOrders({
      orderStatus: this.data.currentOrderStatus,
      lastOrderId: this.data.listData.length > 0 && this.data.listData[0].id || null,
      searchDirection: 'GT',
    }).then(resp => {
      let dataList = resp.content;
      dataList = dataList.concat(this.data.listData);
      this.setData({
        listData: dataList,
      });
      wx.stopPullDownRefresh();
    }).catch(err => {
      wx.stopPullDownRefresh();
      console.log(err);
      wx.showToast({ title: '服务器被大风吹跑了', icon: 'none', duration: 2000 });
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  tapOrderStatus(e) {
    this.setData({
      currentOrderStatus: e.currentTarget.dataset.orderStatus,
    });

    FindUserOrders({
      orderStatus: this.data.currentOrderStatus,
    }).then(resp => {
      this.setData({
        listData: resp.content,
      });
    }).catch(err => {
      console.log(err);
      wx.showToast({ title: '服务器被大风吹跑了', icon: 'none', duration: 2000 });
    })
  },
  loadMore(e) {
    FindUserOrders({
      orderStatus: this.data.currentOrderStatus,
      lastOrderId: this.data.listData.length > 0 && this.data.listData[this.data.listData.length - 1].id || null,
      searchDirection: 'LT',
    }).then(resp => {
      let dataList = resp.content;
      dataList = this.data.listData.concat(dataList);
      this.setData({
        listData: dataList,
      });
    }).catch(err => {
      console.log(err);
      wx.showToast({ title: '服务器被大风吹跑了', icon: 'none', duration: 2000 });
    })
  },
  toHome() {
    wx.navigateBack({
      delta: 1
    });
  }


})